/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.entity;

import ecom.ejb.EuroConverter;
import java.io.Serializable;
import javax.ejb.Remote;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 * @author François
 */
@Entity
@Remote(LigneRemote.class)
@Table(name = "ligne_commande")
public class Ligne implements LigneRemote {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private LignePK id;
    
    @Column(name = "quantite_ligne")
    private int quantite;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "reference_product", nullable = false, insertable = false, updatable = false)
    private Product product;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_commande", nullable = false, insertable = false, updatable = false)
    private Commande commande;

    public Ligne() {
	this.id = new LignePK();
    }

    /**
     * Constructeur complet.
     */
    public Ligne(int quantiteLigne, Product product, Commande commande) {
	super();
	this.id = new LignePK();
	this.quantite = quantiteLigne;
	this.setProduct(product);
	this.setCommande(commande);
    }

    @Override
    public void addQuantite(int quantite) {
	this.quantite += quantite;
    }

    @Override
    public void removeQuantite(int quantite) {
	this.quantite -= quantite;
    }

    public LignePK getId() {
	return this.id;
    }

    public void setId(LignePK id) {
	this.id = id;
    }

    @Override
    public double getMontant() {
	/*
	 * Variable de retour
	 */
	double retour = 0.0;

	retour = this.getProduct().getPrice() * this.getQuantite();
	retour = EuroConverter.floor(retour, 2);

	/*
	 * Sortie
	 */
	return retour;
    }

    @Override
    public int getQuantite() {
	return this.quantite;
    }

    @Override
    public void setQuantite(int quantiteLigne) {
	this.quantite = quantiteLigne;
    }

    @Override
    public Product getProduct() {
	return this.product;
    }

    @Override
    public void setProduct(ProductRemote product) {
	this.product = (Product) product;

	if (this.id == null) {
	    this.id = new LignePK();
	}
	if (product != null
		&& product.getReference() != null
		&& (this.id.getReferenceProduct() != null || !product
			.getReference().equals(this.id.getReferenceProduct()))) {
	    this.id.setReferenceProduct(product.getReference());
	}
    }

    @Override
    public CommandeRemote getCommande() {
	return this.commande;
    }

    @Override
    public void setCommande(CommandeRemote commande) {
	this.commande = (Commande) commande;

	if (this.id == null) {
	    this.id = new LignePK();
	}
	if (commande != null
		&& commande.getId() != null
		&& (this.id.getIdCommande() == null || !commande.getId()
			.equals(this.id.getIdCommande()))) {
	    this.id.setIdCommande(commande.getId());
	}
    }

    @Override
    public int hashCode() {
	int prime = 31;
	int result = 1;
	result = prime
		* result
		+ ((commande == null || commande.getId() == null) ? 0
			: commande.getId().hashCode());
	result = prime * result + ((id == null) ? 0 : id.hashCode());
	result = prime
		* result
		+ ((product == null || product.getReference() == null) ? 0
			: product.getReference().hashCode());
	result = prime * result + quantite;
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (!(obj instanceof Ligne)) {
	    return false;
	}
	Ligne other = (Ligne) obj;
	if (commande == null) {
	    if (other.commande != null) {
		return false;
	    }
	} else if (commande.getId() == null) {
	    if (other.commande.getId() != null) {
		return false;
	    }
	} else if (!commande.getId().equals(other.commande.getId())) {
	    return false;
	}
	if (id == null) {
	    if (other.id != null) {
		return false;
	    }
	} else if (!id.equals(other.id)) {
	    return false;
	}
	if (product == null) {
	    if (other.product != null) {
		return false;
	    }
	} else if (!product.getReference().equals(other.product.getReference())) {
	    return false;
	}
	if (quantite != other.quantite) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	return "Ligne ["
		+ (id != null ? "id=" + id + ", " : "")
		+ "quantite="
		+ quantite
		+ ", "
		+ (product != null ? "product=" + product.getReference() + ", "
			: "")
		+ (commande != null ? "commande=" + commande.getId() : "")
		+ "]";
    }
}